package com.lun.swordtowardoffer2.c06;

import java.util.LinkedList;

public class EvalReversePolishNotation {

	public int eval(String[] tokens) {
		LinkedList<Integer> stack = new LinkedList<>();
		for (String token : tokens) {
			switch (token) {
			case "+":
			case "-":
			case "*":
			case "/":
				int num1 = stack.pop();
				int num2 = stack.pop();
				stack.push(calc(num1, num2, token));
				break;
			default:
				stack.push(Integer.valueOf(token));
			}
		}
		return stack.pop();
	}

	private int calc(int num1, int num2, String operator) {
		switch (operator) {
		case "+":
			return num1 + num2;
		case "-":
			return num1 - num2;
		case "*":
			return num1 * num2;
		case "/":
			return num1 / num2;
		default:
			return 0;
		}
	}

}
